module rank {

    export class BigRankItem extends egret.DisplayObjectContainer {

        private rankNmTxt: egret.TextField;

        private headImg: egret.Bitmap;

        private nickTxt: egret.TextField;

        private scoreTxt: egret.TextField;

        private bgImg: egret.Bitmap;

        public static WIDTH: number = 550;

        public static HEIGHT: number = 88;

        private info: ScoreItem;

        private imageLoader: egret.ImageLoader;

        private medalImg: egret.Bitmap;

        public dispose(): void {
            if (this.imageLoader) {
                this.imageLoader.removeEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            }
            this.imageLoader = null;
        }

        public static readonly VISIBLE: number = 0;
        public static readonly INVISIBLE: number = 1;

        private bgVisible: number = 0;

        public constructor(info: ScoreItem, bgVisible: number) {
            super();
            this.info = info;
            this.bgVisible = bgVisible;
            if (info != null) {
                this.init();
            }
        }

        public static readonly RIGHT_X: number = 20;
        public static readonly LEFT_X: number = 20;

        private init(): void {
            // this.bgImg = new egret.Bitmap(CommonRank.spriteSheet.getTexture("rank_item"));
            // this.addChild(this.bgImg);

            // if (this.bgVisible == BigRankItem.INVISIBLE) {
            //     this.bgImg.visible = false;
            // }

            let info = this.info;
            this.rankNmTxt = new egret.TextField();
            this.rankNmTxt.size = 60;
            let rankIdx: number = info.rankNb;
            this.rankNmTxt.visible = false;
            if (rankIdx < 2) {
                // this.rankNmTxt.textColor = 0xff0000;
                this.medalImg = new egret.Bitmap(CommonRank.spriteSheet.getTexture("rank_medal_1"));
            } else if (rankIdx < 3) {
                // this.rankNmTxt.textColor = 0xffa800;
                this.medalImg = new egret.Bitmap(CommonRank.spriteSheet.getTexture("rank_medal_2"));
            } else if (rankIdx < 4) {
                // this.rankNmTxt.textColor = 0xfffc00;
                this.medalImg = new egret.Bitmap(CommonRank.spriteSheet.getTexture("rank_medal_3"));
            } else {
                this.rankNmTxt.visible = true;
                this.rankNmTxt.textColor = 0x4d4d4d;
            }

            this.rankNmTxt.text = info.rankNb.toString();
            this.addChild(this.rankNmTxt);

            this.headImg = new egret.Bitmap();
            this.headImg.width = this.headImg.height = 65;
            this.addChild(this.headImg);

            if (info.nick.length > 10) {
                info.nick = info.nick.substr(0, 10);
            }

            this.nickTxt = new egret.TextField();
            // this.nickTxt.cacheAsBitmap = true;
            this.nickTxt.textColor = 0x0;
            this.nickTxt.text = info.nick;
            this.addChild(this.nickTxt);
            this.nickTxt.size = 20;
            this.nickTxt.bold = true;

            this.scoreTxt = new egret.TextField();
            this.scoreTxt.size = 40;
            this.scoreTxt.text = info.score.toString();
            this.addChild(this.scoreTxt);

            this.rankNmTxt.x = 10;

            this.headImg.x = BigRankItem.LEFT_X + 50;

            this.nickTxt.x = this.headImg.x + this.headImg.width + 10  + 50;

            this.scoreTxt.x = BigRankItem.WIDTH - this.scoreTxt.width + 150 >> 1;

            // this.rankNmTxt.x = BigRankItem.WIDTH - this.rankNmTxt.width - BigRankItem.RIGHT_X;
            this.rankNmTxt.x = 480 + (40 - this.rankNmTxt.width) / 2;

            this.rankNmTxt.y = BigRankItem.HEIGHT - this.rankNmTxt.height >> 1;
            this.headImg.y = BigRankItem.HEIGHT - this.headImg.height >> 1;
            this.nickTxt.y = BigRankItem.HEIGHT - this.nickTxt.height >> 1;
            this.scoreTxt.y = BigRankItem.HEIGHT - this.scoreTxt.height >> 1;
            this.scoreTxt.textColor = 0xa76f40;

            if (this.medalImg != null) {
                this.addChild(this.medalImg);
                this.medalImg.x = 480 + (40 - this.medalImg.width) / 2;
                this.medalImg.y = BigRankItem.HEIGHT - this.headImg.height >> 1;
            }


            this.loadImg();
        }

        public static getMedalTitle(score: number): string {
            let level: number = Math.floor(score / 1000);
            if (level >= Main.MEDALS.length) {
                level = Main.MEDALS.length - 1;
            }
            return Main.MEDALS[level];
        }

        public loadImg() {
            this.imageLoader = new egret.ImageLoader();
            this.imageLoader.addEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            this.imageLoader.load(this.info.avatarUrl);
        }

        private loadCompleteHandler(event: egret.Event): void {
            let imageLoader = <egret.ImageLoader>event.currentTarget;
            imageLoader.removeEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            let texture = new egret.Texture();
            texture._setBitmapData(imageLoader.data);
            this.headImg.texture = texture;
        }

    }

}